﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using CP.Controllers.GPM;
using CP.Entities;
using CP.Utility;

namespace GPM
{
    public partial class FrmMenuRightCopy : BaseForm
    {
        private int MenuFrom;
        private int MenuTo;
        public FrmMenuRightCopy()
        {
            InitializeComponent();
            this.BarComponents = components;
        }

        private void btnResourceQuery_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(editResourceName.Text))
            {
                ExMessageBox.Show("菜单名称不能为空", AppSetter.SoftName);
                return;
            }
            try
            {
                UserFuncController uc = new UserFuncController();
                DataTable dt = uc.GetResourceInfo("0",
                    this.editResourceName.Text);

                gcResource.DataSource = dt;
            }
            catch (Exception ex)
            {
                ExMessageBox.Show("提示信息：错误信息[" + ex.Message + "]", AppSetter.SoftName);
                this.ActiveControl = this.editResourceName;
            }
        }

        private void FrmMenuRightCopy_Load(object sender, EventArgs e)
        {
            this.ActiveControl = this.editResourceName;
        }

        private void editResourceName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnResourceQuery.PerformClick();
            }
        }

        private void btnQueryFrom_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtMenuFrom.Text))
            {
                ExMessageBox.Show("资源名不能为空", AppSetter.SoftName);
                return;
            }

            try
            {
                UserFuncController uc = new UserFuncController();
                DataTable dt = uc.GetResourceInfo("0",
                    this.txtMenuFrom.Text);

                gcMenuFrom.DataSource = dt;
            }
            catch (Exception ex)
            {
                ExMessageBox.Show("提示信息：错误信息[" + ex.Message + "]", AppSetter.SoftName);
                this.ActiveControl = this.txtMenuFrom;
            }
        }

        private void gvResource_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gvResource.GetFocusedDataRow();
            if (null == dr)
                return;
            this.txtMenuCopyTo.Text = dr["resource_name"] == null ? "" : dr["resource_name"].ToString();
            this.MenuTo = Convert.ToInt32(dr["resource_id"] ?? "0");
        }

        private void txtMenuFrom_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnQueryFrom.PerformClick();
            }
        }

        private void gvMenuFrom_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gvMenuFrom.GetFocusedDataRow();
            if (null == dr)
                return;
            this.txtMenuCopyFrom.Text = dr["resource_name"] == null ? "" : dr["resource_name"].ToString();
            this.MenuFrom = Convert.ToInt32(dr["resource_id"] ?? "0");
        }

        private bool CheckInput()
        {
            if (this.MenuFrom == 0)
            {
                ExMessageBox.Show("提示信息：没有选中要拷贝的菜单", AppSetter.SoftName);
                return false;
            }
            if (this.MenuTo == 0)
            {
                ExMessageBox.Show("提示信息：没有选中要授权的菜单", AppSetter.SoftName);
                return false;
            }
            return true;
        }

        private void btnAuth_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                if (ExMessageBox.Confirm("提示信息：您确定要拷贝菜单权限吗？", AppSetter.SoftName) == DialogResult.No)
                    return;
                try
                {
                    ResourceAuthController ctler = new ResourceAuthController();
                    ctler.MenuRightCopy(this.MenuFrom, this.MenuTo);
                    LogHelper.Log("菜单权限拷贝", "copy", "MenuTo:" + MenuTo, "MenuFrom:" +
                        MenuFrom);
                    ExMessageBox.Show("提示信息：菜单权限拷贝成功！", AppSetter.SoftName);
                }
                catch (Exception ex)
                {
                    ExMessageBox.Show(ex.Message, AppSetter.SoftName);
                }
            }
        }

    }
}